<?php

namespace App\Model\Business;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Demoment extends Model
{
    # 数据表
    protected $table = 'demoments';

    # PK
    protected $primaryKey = 'formuid';

    # fillable fields
    protected $fillable = [
        'status',
        'chinesename',
        'fullname',
        'dateofbirth',
        'sex',
        'gradeno',
        'curriculum',
        'schoolname',
        'textbookname',
        'needsubject',
        'firstLessonContent',
        'difficulty',
        'currentgrades',
        'classdate',
        'lessondate',
        'email',
        'mobile',
        'studentuid',
        'ClassInNo',
        'signupDate',
        'remark1',
        'remark2',
        'remark3'
    ];


    /**
     * 获取所有列表
     * @param Request $request
     * @return mixed
     */
    public static function getAll(Request $request)
    {
        $query = DB::table('demoments as app')
            ->leftjoin('students', 'students.studentuid', '=', 'app.studentuid');
        if($request->has('status')) {
            $status = intval($request->get('status'));
            $query->where('app.status',  $status);
        }
        # 构造查询条件
        if($request->has('chinesename')) {
            $name = "%{$request->chinesename}%";
            $query->orwhere('app.chinesename', 'like', $name);
        }
        if($request->has('ClassIn')) {
            $ClassIn = "%{$request->ClassIn}%";
            $query->orwhere('students.ClassInNo', 'like', $ClassIn);
        }
        # 需要／不需要 分页
        if($request->has('page')) {
            $limit = 30;
            if ($request->has('perPage')) {
                $limit = $request->get('perPage');
            }
            $items = $query->paginate($limit)->toarray();
        }else{
            $items = $query->get()->toarray();
        }
        return $items;
    }

}
